package com.sfh.cpc.com.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sfh.cpc.com.R;


/**
 * 功能描述: 标题
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/4/10
 */
public class WidgetBaseToolbar extends FrameLayout {

    ImageView ivBack;
    public WidgetBaseToolbar(@NonNull Context context) {
        super(context);
        init(null);
    }

    public WidgetBaseToolbar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public WidgetBaseToolbar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    public ImageView getIvBack() {
        return ivBack;
    }

    private void init(AttributeSet attrs) {
        this.setBackFinish();

    }

    public void addLine(){
        TextView tv= new TextView(this.getContext());
        tv.setBackgroundColor(Color.parseColor("#e3e3e3"));
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.height = 1;
        params.gravity = Gravity.CENTER|Gravity.BOTTOM;
        this.addView(tv, params);
    }
    /***
     * 设置标题（默认大小14sp,白色字体）
     * @param title
     * @return
     */
    public TextView setTitle(CharSequence title) {
        TextView tvTitle = new TextView(getContext());
        tvTitle.setText(title);
        tvTitle.setTextSize(18.0F);
        tvTitle.setTextColor(Color.BLACK);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        this.addView(tvTitle, params);
        return tvTitle;
    }

    public ImageView setBackFinish() {
        if (ivBack == null){
            ivBack = new ImageView(getContext());
            ivBack.setImageResource(R.drawable.ic_common_back);
            ivBack.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context activity = getContext();
                    if (activity instanceof Activity) {
                        ((Activity) activity).finish();
                    }
                }
            });
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER | Gravity.LEFT;
            this.addView(ivBack, params);
        }
        return ivBack;
    }

    public ImageView setRightView(@DrawableRes int res) {
        ImageView ivRight = new ImageView(getContext());
        ivRight.setImageResource(res);
        ivRight.setPadding(7, 7, 7, 7);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER | Gravity.RIGHT;
        params.rightMargin = 20;
        this.addView(ivRight, params);
        return ivRight;
    }

    public TextView setRightView(CharSequence texts) {
        TextView tvRight = new TextView(getContext());
        tvRight.setText(texts);
        tvRight.setTextSize(16.0F);
        tvRight.setTextColor(Color.BLACK);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER | Gravity.RIGHT;
        params.rightMargin = 20;
        this.addView(tvRight, params);
        return tvRight;
    }

    public LinearLayout setLeftLayout() {
        LinearLayout lyLeft = new LinearLayout(getContext());
        lyLeft.setGravity(Gravity.CENTER);
        lyLeft.setOrientation(LinearLayout.HORIZONTAL);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER | Gravity.LEFT;
        this.addView(lyLeft, params);
        return lyLeft;
    }

    public LinearLayout setRightLayout() {
        LinearLayout lyRight = new LinearLayout(getContext());
        lyRight.setGravity(Gravity.CENTER);
        lyRight.setOrientation(LinearLayout.HORIZONTAL);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER | Gravity.RIGHT;
        this.addView(lyRight, params);
        return lyRight;
    }

    public LinearLayout setCenterLayout() {
        LinearLayout lyCenter = new LinearLayout(getContext());
        lyCenter.setGravity(Gravity.CENTER);
        lyCenter.setOrientation(LinearLayout.HORIZONTAL);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        this.addView(lyCenter, params);
        return lyCenter;
    }
}
